package com.hotel.front;

import java.io.IOException;
import java.text.ParseException;
import java.util.List;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.Util;
import com.google.appengine.api.datastore.EntityNotFoundException;

import com.google.gson.Gson;
import com.hotel.BL.SubPaymentLogic;

import data.SubPayment;
import data.passingData.ResultData;



@SuppressWarnings("serial")
public class SubPaymentService extends HttpServlet{	
	//http://localhost:8080/paymentService?func=addPayment&title=1&shortname=1&mode=1&description=aa
	//http://localhost:8080/PaymentService?func=editPayment&&PaymentId=865&title=222222222222222&shortname=1&mode=1&description=aaaaa
	//http://localhost:8080/PaymentService?func=delPayment&PaymentId=865
	//http://localhost:8080/PaymentService?func=changePayment&PaymentId=865
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		try{
			String doClass = req.getParameter("func");
			if(doClass.equals("addRoutePayment")){
				AddRoutePayment(req,resp);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void AddRoutePayment(HttpServletRequest req, HttpServletResponse resp)
			throws ParseException, IOException, EntityNotFoundException {
		Gson gson = new Gson();	
		HttpSession session=req.getSession(true);
		long hotelId = (Long) session.getAttribute("hotelId");
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long folioId = Long.parseLong(req.getParameter("folioId"));
		double amount= Double.parseDouble(req.getParameter("amount"));
		String PaymentList = req.getParameter("PaymentList");
		List<SubPayment> g = new SubPaymentLogic(req,resp).RoutePayment(PaymentList, amount, folioId, hotelId);
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(g));
	}
}

